'use strict';

const Controller = require('egg').Controller;
const qr = require('qr-image');

class QRCodeController extends Controller {
  async index() {
    const { ctx } = this;
    const { text, size, margin } = ctx.query;
    try {
      // 大小默认5，二维码周围间距默认1
      const img = qr.image(text || '', {
        type: 'png',
        size: size || 5,
        margin: margin || 1,
      });
      ctx.status = 200;
      ctx.type = 'image/png';
      ctx.body = img;
    } catch (e) {
      ctx.status = 414;
      ctx.set('Content-Type', 'text/html');
      ctx.body = '<h1>414 Request-URI Too Large</h1>';
    }
  }
}

module.exports = QRCodeController;
